package com.warm.pump.module.skd.service.impl;

import com.warm.pump.frame.gen.bean.PageBean;
import com.warm.pump.module.skd.bean.po.gen.ScheduleJobDtl;
import com.warm.pump.module.skd.dao.gen.ScheduleJobDtlDao;
import com.warm.pump.module.skd.dao.gen.SkdBoDao;
import com.warm.pump.module.skd.service.IScheduleJobDtlService;
import com.warm.pump.module.work.bean.WorkTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service("ScheduleJobDtlService")
public class ScheduleJobDtlServiceImpl implements IScheduleJobDtlService {
    @Autowired
    private ScheduleJobDtlDao scheduleJobDtlDao;
    @Autowired
    private SkdBoDao skdBoDao;

    @Override
    public ScheduleJobDtl queryObject(String id) {
        return scheduleJobDtlDao.selectByPrimaryKey(id);
    }

    public ScheduleJobDtl queryObject(Map<String, Object> map) {
        return scheduleJobDtlDao.selectByMap(map);
    }

    @Override
    public List<ScheduleJobDtl> queryList(Map<String, Object> map) {
        return scheduleJobDtlDao.getListByMap(map);
    }

    @Override
    public PageBean getPageBeanByMap(Map map, int pageNo, int pageSize) {
        return scheduleJobDtlDao.getPageBeanByMap(map, pageNo, pageSize);
    }


    @Override
    public int queryTotal(Map<String, Object> map) {
        return scheduleJobDtlDao.countTotalByMap(map);
    }

    @Override
    public void save(ScheduleJobDtl scheduleJobDtl) {
        Map<String, Object> map = new HashMap<>();
        map.put("jobId", scheduleJobDtl.getJobId());
        map.put("pid", scheduleJobDtl.getPid());
        map.put("type", scheduleJobDtl.getType());
        scheduleJobDtl.setExecNum(skdBoDao.selectJobDtlNextExecNum(map));
        scheduleJobDtlDao.insertSelective(scheduleJobDtl);
    }


    /**
     * 保存任务明细
     *
     * @param jobId
     * @param itemTrees
     */
    public void saveJobDtl(Long jobId, List<WorkTree> itemTrees) {
        List<ScheduleJobDtl> jobDtls = new ArrayList<>();
        for (WorkTree itemTree : itemTrees) {
            ScheduleJobDtl sjobDtl = this.queryObject(jobId + itemTree.getId());
            if (null == sjobDtl) {
                ScheduleJobDtl jobDtl = new ScheduleJobDtl();
                jobDtl.setId(jobId + itemTree.getId());
                jobDtl.setJobId(jobId);
                jobDtl.setType(itemTree.getType());
                Long workId = Long.parseLong(itemTree.getId().replace("work_", "").replace("workSubject_", ""));
                jobDtl.setWorkId(workId);
                jobDtl.setPid(jobId + itemTree.getParentId());
                jobDtl.setExecNum(0);
                jobDtl.setCreateTime(new Date());
                jobDtl.setUpdateTime(new Date());
                this.save(jobDtl);
            }
        }
    }

    @Override
    public void update(ScheduleJobDtl scheduleJobDtl) {
        scheduleJobDtlDao.updateByPrimaryKeySelective(scheduleJobDtl);
    }

    @Override
    public void delete(String id) {
        scheduleJobDtlDao.deleteByPrimaryKey(id);
    }

    @Override
    public void delete(Map<String, Object> map) {
        scheduleJobDtlDao.deleteByMap(map);
    }

    @Override
    public void deleteBatch(String[] ids) {
        Map<String, Object> map = new HashMap<>();
        map.put("id_in", ids);

        for (String id : ids) {
            ScheduleJobDtl jobDtl = queryObject(id);

            if ("workSubject".equals(jobDtl.getType())) {
                Map<String, Object> itemMap = new HashMap<>();
                itemMap.put("pid", jobDtl.getId());
                scheduleJobDtlDao.deleteByMap(itemMap);
            } else if ("workItem".equals(jobDtl.getType())) {
                Map<String, Object> itemMap = new HashMap<>();
                itemMap.put("pid", jobDtl.getPid());
                if (queryTotal(itemMap) <= 1) {
                    scheduleJobDtlDao.deleteByPrimaryKey(jobDtl.getPid());
                }
            }
        }
        scheduleJobDtlDao.deleteByMap(map);
    }

}
